import Home from '../components/Home.vue'
import FAQ from '../components/FAQ.vue'
import VueRouter from 'vue-router'
import Vue from "vue"
import Login from "../components/Login.vue"
import TicketsLayout from '../components/TicketsLayout.vue'
import state from '../state'

Vue.use(VueRouter)

const routes=[
    {path:'/',name:'home',component:Home},
    {path:'/faq',name:'faq',component:FAQ},
    {path:'/login',name:'login',component:Login,meta:{guest:true}},
    {path:'/tickets',name:'tickets',component:TicketsLayout,meta:{private:true}}
]

const router=new VueRouter({routes,})

//路由守卫
router.beforeEach((to,from,next)=>{
    next()
    if(to.meta.private && !state.user){
        alert("Please login first")
        next({name:'login',
              params:{
                  wantedRoute:to.fullPath,
              }})
        return
    }
    if(to.meta.guest && state.user){
        next({name:'home'})
        return
    }

   
})

export default router